/*  File Name	: Lock.cpp
 *  Author		: Pang Jiacai
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-3-22
 *  Description:
 */

#include "Lock.h"

Lock::Lock()
{

}

Lock::~Lock()
{

}

RecursiveThreadMutex_t::RecursiveThreadMutex_t()
{
	this->owner_id_ =-1;
	pthread_mutex_init(&this->p_mutex_,NULL);
}

RecursiveThreadMutex_t::~RecursiveThreadMutex_t()
{

}

RecursiveThreadMutex::RecursiveThreadMutex()
{

}

RecursiveThreadMutex::~RecursiveThreadMutex()
{

}

int RecursiveThreadMutex::remove(void)
{
	DEBUG_LOCK("Recursive_Thread_Mutex::remove");
	return ::pthread_mutex_destroy(&this->lock_.p_mutex_);
}

int RecursiveThreadMutex::acquire(void)
{
	DEBUG_LOCK("Recursive_Thread_Mutex::acquire");
	pthread_t thr_id = ::pthread_self();
	this->set_thread_id(thr_id);
	return ::pthread_mutex_lock(&this->lock_.p_mutex_);
}

int RecursiveThreadMutex::acquire(int sec,int nsec)
{
	DEBUG_LOCK("Recursive_Thread_Mutex::acquire with time");
	 timespec ts;
	 ts.tv_sec = sec;
	 ts.tv_nsec = nsec;
	return ::pthread_mutex_timedlock(&this->lock_.p_mutex_,&ts);
}

int RecursiveThreadMutex::acquire_read(void)
{
	return 0;
}

int RecursiveThreadMutex::acquire_write(void)
{
	return 0;
}

int RecursiveThreadMutex::tryacquire(void)
{
	DEBUG_LOCK("Recursive_Thread_Mutex::tryacquire");
	return ::pthread_mutex_trylock(&this->lock_.p_mutex_);
}

int RecursiveThreadMutex::tryacquire_read(void)
{
	return 0;
}

int RecursiveThreadMutex::tryacquire_write(void)
{
	return 0;
}

int RecursiveThreadMutex::release(void)
{
	DEBUG_LOCK("Recursive_Thread_Mutex::release");
	return ::pthread_mutex_unlock(&this->lock_.p_mutex_);
}

RecursiveThreadMutex_t &RecursiveThreadMutex::mutex(void)
{
	return this->lock_;
}

pthread_t RecursiveThreadMutex::get_thread_id(void)
{
	return this->lock_.owner_id_;
}

void RecursiveThreadMutex::set_thread_id(pthread_t t)
{
	this->lock_.owner_id_ = t;
}
